import myAxios from "@/utils/myAxios"
import userStore from "@/store/user"

const BaseUrl = '/api/todo'

/**
 * 新增todo
 * @param todoText
 * @returns {Promise<AxiosResponse<T>>}
 */
export async function addToDo(todoText) {
    return myAxios.post(`${BaseUrl}/add/${todoText}`, null, {
        headers: {
            Authorization: "Bearer " + userStore.state.token,
        }
    })
}

/**
 * 获取todo列表
 * @returns {Promise<AxiosResponse<any>>}
 */
export async function getToDoList() {
    return myAxios.get(`${BaseUrl}/get/list`, {
        headers: {
            Authorization: "Bearer " + userStore.state.token,
        }
    })
}

/**
 * 编辑todo
 * @param todoInfo
 * @returns {Promise<AxiosResponse<T>>}
 */
export async function editToDoReq(todoInfo) {
    return myAxios.post(`${BaseUrl}/edit`, {
        id: todoInfo.id,
        text: todoInfo.text,
        endTime: new Date(todoInfo.endTime),
        beginTime: new Date(todoInfo.beginTime)
    }, {
        headers: {
            Authorization: "Bearer " + userStore.state.token,
        }
    })
}

export async function finishToDoReq(id) {
    return myAxios.post(`${BaseUrl}/finish/${id}`, null, {
        headers: {
            Authorization: "Bearer " + userStore.state.token,
        }
    })
}

/**
 * 获取完成的todo列表
 * @returns {Promise<AxiosResponse<any>>}
 */
export async function getFinishedToDoList() {
    return myAxios.get(`${BaseUrl}/get/finished/list`, {
        headers: {
            Authorization: "Bearer " + userStore.state.token,
        }
    })
}

/**
 * 获取超时的todo列表
 * @returns {Promise<AxiosResponse<any>>}
 */
export async function getExpiredToDoList() {
    return myAxios.get(`${BaseUrl}/get/expired/list`, {
        headers: {
            Authorization: "Bearer " + userStore.state.token,
        }
    })
}

/**
 * 获取barChart信息
 * @returns {Promise<AxiosResponse<any>>}
 */
export async function getBarChartInfo() {
    return myAxios.get(`${BaseUrl}/get/chart/bar`, {
        headers: {
            Authorization: "Bearer " + userStore.state.token,
        }
    })
}


